package org.example.Exception;

import lombok.extern.slf4j.Slf4j;
import org.example.Pojo.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result globalExceptionHandler(Exception e) {
        log.error(e.getMessage());
        return Result.error(e.getMessage());
    }


    @ExceptionHandler
    public Result SQLIntegrityConstraintViolationExceptionHandler(SQLIntegrityConstraintViolationException e) {
        log.error(e.getMessage());
        String message = e.getMessage();
        int i=message.indexOf("Duplicate entry");
        message=message.substring(i);
        log.info(message);
        message = message.split(" ")[2];
        return Result.error(message+"重复了");
    }


    @ExceptionHandler
    public Result StudentExistExceptionHandler(StudentExistException e) {
        log.error(e.getMessage());
        return Result.error("对不起, 该班级下有学生, 不能直接删除");
    }

    @ExceptionHandler
    public Result EmpExistExceptionHandler(EmpExistException e) {
        log.error(e.getMessage());
        return Result.error("对不起，当前部门下有员工，不能直接删除！");
    }

    @ExceptionHandler
    public Result StudentNoExistExceptionHandler(StudentNoExistException e) {
        log.error(e.getMessage());
        return Result.error("账号或者密码不正确");
    }
}
